<template>
  <div style="background-color: #c3ddec;display: flex;align-items: center;justify-content: center;height: 80vh;">
    <el-card class="box-card" >
      <el-input
          v-model="inputValue"
          placeholder="粘贴任意内容"
          type="textarea"
          autosize
          style="width: 300px; height: 50px;"
      ></el-input>
    </el-card>
    <el-card>
      <div>
        <h3>提取文件</h3>
        <el-form ref="formRef" :model="formData" label-width="100px">
          <el-form-item label="链接">
            <el-input v-model="formData.shareUrl" placeholder="请输入链接"></el-input>
          </el-form-item>
          <el-form-item label="提取码">
            <el-input v-model="formData.code" placeholder="请输入提取码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="fetchDownloadUrl">提取</el-button>
          </el-form-item>
        </el-form>
        <div v-if="downloadUrl" class="mt-2">
          <span>下载链接：</span>
          <a :href="downloadUrl" target="_blank" style="color: #0c95f7">{{ downloadUrl }}</a>
          <span>(点击进行下载)</span>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import request from '@/utils/request.js';
import { ElMessage } from 'element-plus';

const inputValue=ref()

const formData = ref({
  shareUrl: '',
  code: ''
});
const downloadUrl = ref('');

const fetchDownloadUrl = async () => {
  try {
    const res = await request.post('/share/find', formData.value);
    console.log(res)
    if (res.code === '200') {
      downloadUrl.value = res.data.downUrl;
    } else {
      ElMessage.error("信息错误或者已经过期");
    }
  } catch (error) {
    ElMessage.error('请求失败，文件已经过期或者源文件损坏');
  }
};
</script>

<style lang="scss" scoped>
.mt-2 {
  margin-top: 10px;
}
</style>